package com.wd.cloud.applet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wd.cloud.applet.common.constant.dataverify.Constant;
import com.wd.cloud.applet.common.exception.CustomException;
import com.wd.cloud.applet.config.CustomListResultConfig;
import com.wd.cloud.applet.config.ModelMapperConfig;
import com.wd.cloud.applet.config.MybatisPlusPageConfig;
import com.wd.cloud.applet.entity.bo.SysRoleMenuInfoBO.SysRoleMenuInfoListBO;
import com.wd.cloud.applet.entity.bo.SysRoleMenuInfoBO.SysRoleMenuInfoSaveBO;
import com.wd.cloud.applet.entity.commons.RequestPrimaryKeyModel;
import com.wd.cloud.applet.entity.dto.SysRoleMenuInfoModel;
import com.wd.cloud.applet.entity.vo.SysRoleMenuInfoVO.SysRoleMenuInfoKeyVO;
import com.wd.cloud.applet.entity.vo.SysRoleMenuInfoVO.SysRoleMenuInfoListVO;
import com.wd.cloud.applet.mapper.SysRoleMenuInfoMapper;
import com.wd.cloud.applet.service.ISysRoleMenuInfoService;
import com.wd.cloud.applet.utils.HelperUtils;
import com.wd.cloud.applet.utils.string.StringUtils;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

/**
* Description: [角色菜单关联表服务实现]
* Created on 2023年04月21日
* @author  <a href="mailto: 987707681@qq.com">hjx</a>
* @version 1.0
* Copyright (c) 2023年 代码生成器
*/
@Service
public class SysRoleMenuInfoServiceImpl extends ServiceImpl<SysRoleMenuInfoMapper, SysRoleMenuInfoModel> implements ISysRoleMenuInfoService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private SysRoleMenuInfoMapper appletMapper;

    /**
     * 根据主键ID删除数据（逻辑删除）
     *
     * @param params
     * @return
     */
    @Override
    public int deleteByPrimaryKey(RequestPrimaryKeyModel params) {
        if (StringUtils.isNullOrTrimEmpty(params.getId())) {
            throw new CustomException("请选择需要删除的数据", 100);
        }
        SysRoleMenuInfoModel model = appletMapper.selectById(params.getId());
        if (model == null) {
            throw new CustomException("删除的数据不存在", 100);
        }
        return appletMapper.updateById(model);
    }

    /**
     * 新增数据
     *
     * @param params
     * @return
     */
    @Override
    public int insertSelective(SysRoleMenuInfoSaveBO params) {
        SysRoleMenuInfoModel model = ModelMapperConfig.mapTo(SysRoleMenuInfoModel.class, params);
        model.setId(HelperUtils.getUuid());
        return appletMapper.insert(model);
    }

    /**
     * 新增或修改
     *
     * @param params
     * @return
     */
    @Override
    public int save(SysRoleMenuInfoSaveBO params) {
        SysRoleMenuInfoModel model = ModelMapperConfig.mapTo(SysRoleMenuInfoModel.class, params);
        if (model != null) {
            if (model.getId() == null || model.getId().isEmpty()) {
                model.setId(HelperUtils.getUuid());
                return appletMapper.insert(model);
            } else {
                return appletMapper.updateById(model);
            }
        }
        return 0;
    }

    /**
     * 分页获取数据列表
     *
     * @param params
     * @return
     */
    @Override
    public CustomListResultConfig<SysRoleMenuInfoListVO> selectByPageList(
        SysRoleMenuInfoListBO params) {
        LambdaQueryWrapper<SysRoleMenuInfoModel> userLambdaQueryWrapper = Wrappers.lambdaQuery();
        Page<SysRoleMenuInfoModel> page = new Page<>(params.getPage(), params.getLimit());
        IPage<SysRoleMenuInfoModel> entityIPage =
            appletMapper.selectPage(page, userLambdaQueryWrapper);
        CustomListResultConfig<SysRoleMenuInfoListVO> responseDtoList =
            MybatisPlusPageConfig.toPageResult(entityIPage, SysRoleMenuInfoListVO.class);
        if (responseDtoList.getList().size() <= 0) {
            throw new CustomException("查无数据", 102);
        }
        return responseDtoList;
    }

    /**
     * 根据主键id获取实体
     *
     * @param params
     * @return
     */
    @Override
    public SysRoleMenuInfoKeyVO selectByPrimaryKey(RequestPrimaryKeyModel params) {
        if (params.getId() == null || params.getId().isEmpty()) {
            throw new CustomException("Id不能为空", 100);
        }
        SysRoleMenuInfoModel model = appletMapper.selectById(params.getId());
        SysRoleMenuInfoKeyVO responestDto =
            ModelMapperConfig.mapTo(SysRoleMenuInfoKeyVO.class, model);
        if (responestDto == null) {
            throw new CustomException("Id错误", 100);
        }
        return responestDto;
    }

}
